package cn.android.weblog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cn.android.weblog.adapter.PassageListAdapter;
import cn.android.weblog.api.CatagoryAPI;
import cn.android.weblog.api.PassageAPI;
import cn.android.weblog.domain.Passage;
import cn.android.weblog.redisdata.CURRENT;
import cn.android.weblog.redisdata.LOGIN;

public class MyBlogActivity extends AppCompatActivity {

    private List<Passage> passage;
    private PassageAPI passageAPI;
    private PassageListAdapter adapter;
    private ListView lv;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_blog);

        lv=(ListView)findViewById(R.id.myblog_lv);
        back=(ImageView)findViewById(R.id.myblog_back);
        adapter=new PassageListAdapter(getApplicationContext());
        passageAPI=new PassageAPI();

        // 初始化页面
        init();

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyBlogActivity.this,MyActivity.class));
            }
        });

        // 单击进入文章详情页
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 将选中的文章信息存入缓存域中
                CURRENT.PASSAGE=(Passage)adapter.getItem(position);
                Intent intent=new Intent(MyBlogActivity.this,PassageDetailActivity.class);
                startActivity(intent);
            }
        });



    }

    /**
     * 初始化页面
     */
    private void init(){
        passage=passageAPI.findPassageByUserId(LOGIN.USER.getUserId());
        adapter.setData(passage);
        lv.setAdapter(adapter);
    }

}
